<template>
	<view class="home-box">
		<view class="top-fix">
			<!-- 头部搜索 -->
			<view class="my-head">
				<image src="/static/image/logo.png" class="logo" mode=""></image>
				<view class="search">
					<i class="iconfont icon-sousuo"></i><input type="text" style="font-size: 24rpx;"
						placeholder-style="font-size: 24rpx;color: #ff6633;" placeholder="请输入...." />
				</view>
				<view class="btn">搜索</view>
			</view>
			<!-- 可滚动区域 -->
			<view class="scrl-box">
				<scroll-view scroll-x enable-flex class="scrl">
					<view class="item" :class='{active:isChose == -1}' @click="recomClickHandle">推荐</view>
					<template v-for="(item, index) in navList">
						<view :class='["item", isChose == index? "active" : ""]'
							@click="otherClickHandle(item.id,index)"
							:key="index">
							{{item.name}}
						</view>
					</template>
				</scroll-view>
			</view>
		</view>
		<view class="content">
			<!-- 推荐项-组件页面 -->
			<recommend v-if="isChose == -1" :policyDescList="policyDescList"></recommend>
			<!-- 其他项-组件页面 -->
			<cateCompt v-else :categoryId="categoryId" @changeTabName="changeTabName"></cateCompt>
		</view>


	</view>
</template>

<script>
	import base from '@/utils/base.js'
	import request from '@/utils/request.js'
	import topTab from '@/utils/homeData/topTab.js'
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex' //引入mapState
	export default {
		data() {
			return {
				navList: [],
				isChose: -1,
				policyDescList: [],
				categoryId:"",

			}
		},
		computed: {
			...mapState('Home', ['navListStore'])
		},
		created() {
			this.getPageData()
		},
		methods: {
			...mapMutations('Home', ['changeNavListStore', 'changeFocusListStore', 'changeAllDatas']),
			async getPageData() {
				this.navList = topTab
				let res = await request(base.indexData, "GET", {})
				console.log('res.data.data', res.data.data);
				if (res.code == '200') {
					this.changeAllDatas(res.data.data.kingKongModule)
					this.changeNavListStore(res.data.data.kingKongModule.kingKongList)
					this.changeFocusListStore(res.data.data.focusList)
					this.policyDescList = res.data.data.policyDescList
				}

			},
			// 点击推荐
			recomClickHandle() {
				this.isChose = -1
				this.getPageData()
			},
			//点击其他的tab项
			otherClickHandle(id, index) {
				this.categoryId = id
				this.isChose = index // 切换tab
			},
			// 获取子组件传回来值
			changeTabName(nvs){
				this.navList = nvs
			}
		},

	}
</script>

<style lang="less" scoped>
	.home-box{
		width: 100%;
		height: 100%;
		background-color: #eee;
	}
	.top-fix {
		position: fixed;
		background: #fff;
		/* #ifdef MP-WEIXIN */
		top: 0;
		/* #endif */
		/* #ifdef H5 */
		top: 84rpx;
		/* #endif */
		left: 0;
		z-index: 999;
	}

	// 头部
	.my-head {
		display: flex;
		align-items: center;
		font-size: 24rpx;
		padding: 10rpx;

		.logo {
			width: 140rpx;
			height: 40rpx;
		}

		.search {
			border: 2rpx solid #eee;
			border-radius: 10rpx;
			padding: 4rpx 10rpx;
			margin: 0 5rpx;
			flex: 1;
			display: flex;
			align-items: center;
		}

		.btn {
			width: 80rpx;
			text-align: center;
		}
	}

	// 可滑动区域
	.scrl-box {
		padding: 0 20rpx;
	}

	.scrl {
		display: flex;
		white-space: nowrap; // 禁止换行 ，必须使用这个
		height: 50rpx; // 控制下边线位置

		.item {
			// flex: 1;
			display: inline-block;
			margin: 0 20rpx;

			&:nth-child(1) {
				margin-left: 0;
			}
		}

		.active {
			color: #c8241e;
			border-bottom: 2rpx solid #c8241e;
		}
	}
	.content{
		background-color: #fff;
		margin-top: 116rpx;
	}
</style>